package com.hyphenate.chatuidemo.utils;

import android.text.TextUtils;

import com.hyphenate.chatuidemo.db.NewUserDao;
import com.hyphenate.chatuidemo.domain.NewUserEntity;

import java.util.HashMap;
import java.util.Map;

public class UserUtil {
    private static UserUtil instance;
    private Map<String,NewUserEntity> map;
    NewUserDao userDao;
    private UserUtil(){
        map = new HashMap<>();
        userDao = new NewUserDao();
    }

    public static synchronized UserUtil getInstance(){
        if (instance== null){
            instance = new UserUtil();
        }
        return instance;
    }

    public NewUserEntity getUserByImId(String imId){
        if (!TextUtils.isEmpty(imId)){
            if (map.containsKey(imId) && !TextUtils.isEmpty(map.get(imId).getName())){
                return map.get(imId);
            }else {
                NewUserEntity userEntity = userDao.getUserByImId(imId);
                if (userEntity!= null){
                    map.put(imId, userEntity);
                }
                return userEntity;
            }
        }
        return null;
    }

    public void saveUser(NewUserEntity entity){
        userDao.saveContact(entity);
    }
}
